package com.youlian.cloud.log.utils;

import com.dianping.cat.Cat;
import com.dianping.cat.message.Event;
import com.dianping.cat.message.Transaction;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;

/**
 * @author : liukx
 * @create : 2018/6/25 16:27
 * @email : liukx@elab-plus.com
 */
public class CatCrossProcess {

    public static void createConsumerCross(HttpServletRequest request, Transaction transaction) {
        Event crossAppEvent = Cat.newEvent(CatMsgConstants.CONSUMER_CALL_APP, getProviderAppName(request));
        Event crossServerEvent = Cat.newEvent(CatMsgConstants.CONSUMER_CALL_SERVER, request.getRemoteHost());
        Event crossPortEvent = Cat.newEvent(CatMsgConstants.CONSUMER_CALL_PORT, request.getRemotePort() + "");
        crossAppEvent.setStatus(Event.SUCCESS);
        crossServerEvent.setStatus(Event.SUCCESS);
        crossPortEvent.setStatus(Event.SUCCESS);
//        completeEvent(crossAppEvent);
//        completeEvent(crossPortEvent);
//        completeEvent(crossServerEvent);
        transaction.addChild(crossAppEvent);
        transaction.addChild(crossPortEvent);
        transaction.addChild(crossServerEvent);
    }

    public static void completeEvent(Event event) {
        event.complete();
    }

    public static String getProviderAppName(HttpServletRequest request) {
        String appName = request.getParameter(CatMsgConstants.PROVIDER_APPLICATION_NAME);
        if (StringUtils.isEmpty(appName)) {
            String interfaceName = request.getParameter(CatMsgConstants.INTERFACE_NAME);
            appName = interfaceName.substring(0, interfaceName.lastIndexOf('.'));
        }
        return appName;
    }

    /**
     * 串联provider端消息树
     *
     * @param request
     * @param t
     */
    public static void createProviderCross(HttpServletRequest request, Transaction t) {
        Event crossAppEvent = Cat.newEvent(CatMsgConstants.PROVIDER_CALL_APP, request.getHeader(CatMsgConstants.APPLICATION_KEY));
        Event crossServerEvent = Cat.newEvent(CatMsgConstants.PROVIDER_CALL_SERVER, request.getRemoteAddr());    //clientIp
        crossAppEvent.setStatus(Event.SUCCESS);
        crossServerEvent.setStatus(Event.SUCCESS);
//        completeEvent(crossAppEvent);
//        completeEvent(crossServerEvent);
        t.addChild(crossAppEvent);
        t.addChild(crossServerEvent);
    }

    /**
     * 统一设置消息编号的messageId
     */
    public static void createMessageTree() {
        CatMsgContext context = new CatMsgContext();
        Cat.logRemoteCallClient(context);
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        requestAttributes.setAttribute(Cat.Context.PARENT, context.getProperty(Cat.Context.PARENT), 0);
        requestAttributes.setAttribute(Cat.Context.ROOT, context.getProperty(Cat.Context.ROOT), 0);
        requestAttributes.setAttribute(Cat.Context.CHILD, context.getProperty(Cat.Context.CHILD), 0);
        requestAttributes.setAttribute(CatMsgConstants.APPLICATION_KEY, Cat.getManager().getDomain(), 0);
    }
}
